package com.qili.controller.sys;

import com.qili.base.entity.CurrentUser;
import com.qili.core.annotation.Log;
import com.qili.entity.base.BaseCompany;
import com.qili.service.base.BaseCompanyService;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author liuyang
 * @date 2021/1/8
 * @description
 */
@Slf4j
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private BaseCompanyService baseCompanyService;

    @RequestMapping("/index")
    public String index(){
        return "/system/company/companyList";
    }

    @ApiOperation(value = "/getList", httpMethod = "GET", notes = "展示监理单位")
    @GetMapping(value = "/getList")
    @ResponseBody
    @RequiresPermissions("company:show")
    public ReType showRoleList(BaseCompany company, Model model, String page, String limit) {
        return baseCompanyService.show(company, Integer.valueOf(page), Integer.valueOf(limit));
    }

    /**
     * 监理单位新增
     * @param model
     * @return
     */
    @GetMapping(value = "showAddCompany")
    @RequiresPermissions("company:add")
    public String goAddRole(Model model) {
        return "/system/company/addCompany";
    }

    /**
     * 添加监理单位
     * @param company
     * @return
     */
    @ApiOperation(value = "/addCompany", httpMethod = "POST", notes = "添加监理单位")
    @Log(desc = "添加监理单位")
    @PostMapping(value = "/addCompany")
    @ResponseBody
    @RequiresPermissions("company:add")
    public JsonUtil addCompany(BaseCompany company) {
        if (StringUtils.isBlank(company.getBcName())) {
            JsonUtil.error("单位名称不能为空");
        }

        BaseCompany sameNameCompany = new BaseCompany();
        sameNameCompany.setBcName(company.getBcName());
        List<BaseCompany> select = baseCompanyService.select(sameNameCompany);
        if(select.size() != 0){
            return JsonUtil.error("已经存在的单位名称");
        }

        company.setBcCreateTime(DateUtil.getCurrentDateTimeString());
        Subject sub = SecurityUtils.getSubject();
        CurrentUser currentUser = (CurrentUser) sub.getPrincipal();
        company.setBcCreateby(currentUser.getId());
        baseCompanyService.insertSelective(company);
        return JsonUtil.sucess("操作成功");
    }

    /**
     * 监理单位编辑 & 查看
     * @param bcId
     * @param model
     * @param detail
     * @return
     */
    @GetMapping(value = "updateCompany")
    @RequiresPermissions(value = {"company:select","company:update"},logical = Logical.OR)
    public String updateCompany(String bcId, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(bcId)) {
            BaseCompany company = baseCompanyService.selectByPrimaryKey(bcId);
            model.addAttribute("company", company);
        }
        model.addAttribute("detail", detail);
        return "/system/company/updateCompany";
    }

    /**
     * 更新监理单位
     * @param baseCompany
     * @return
     */
    @ApiOperation(value = "/updateCompany", httpMethod = "POST", notes = "更新监理单位")
    @Log(desc = "更新监理单位")
    @PostMapping(value = "updateCompany")
    @ResponseBody
    @RequiresPermissions("company:update")
    public JsonUtil updateCompany(BaseCompany baseCompany) {
        if (baseCompany == null) {
            return JsonUtil.error("获取数据失败");
        }

        BaseCompany oldBaseCompany = baseCompanyService.selectByPrimaryKey(baseCompany);
        if(!oldBaseCompany.getBcName().equals(baseCompany.getBcName())){
            //修改了名称,验证新名称是否有重复:
            BaseCompany sameNameCompany = new BaseCompany();
            sameNameCompany.setBcName(baseCompany.getBcName());
            List<BaseCompany> select = baseCompanyService.select(sameNameCompany);
            if(select.size() > 0){
                return JsonUtil.error("已经存在的监理单位名称");
            }
        }

        baseCompanyService.updateByPrimaryKeySelective(baseCompany);
        return JsonUtil.sucess("操作成功");
    }

    /**
     * 删除监理单位
     * @param bcId
     * @return
     */
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除监理单位")
    @Log(desc = "删除监理单位")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("company:del")
    public JsonUtil del(String bcId) {
        if (StringUtils.isEmpty(bcId)) {
            return JsonUtil.error("获取数据失败");
        }
        baseCompanyService.deleteByPrimaryKey(bcId);
        return JsonUtil.sucess("操作成功");
    }


}
